import { ArticleDetail } from '@/types/data';
import { ArticleAction } from '@/types/store';

// 文章reducer模块
type ArticleState = {
  detail: ArticleDetail;
};

const initialState = {
  detail: {},
} as ArticleState;

export const article = (
  state = initialState,
  action: ArticleAction
): ArticleState => {
  // 具体的action处理, 文章详情
  if (action.type === 'article/getArticleById') {
    return {
      ...state,
      detail: action.payload,
    };
  }

  // 更新文章详情
  if (action.type === 'article/updateInfo') {
    return {
      ...state,
      detail: {
        ...state.detail,
        // 更新对应的值
        [action.payload.name]: action.payload.value,
      },
    };
  }

  return state;
};
